package com.chenglai.video.service.impl;

import com.chenglai.commonutils.common.DateFormatHelper;
import com.chenglai.video.dao.VideoInfoMapper;
import com.chenglai.video.pojo.VideoCategory;
import com.chenglai.video.pojo.VideoInfo;
import com.chenglai.video.service.VideoCategoryService;
import com.chenglai.video.service.VideoInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/16 8:57
 */
@Service
public class VideoInfoServiceImpl implements VideoInfoService {

    @Resource
    private VideoCategoryService videoCategoryService;

    @Resource
    private VideoInfoMapper videoInfoMapper;

    @Override
    public PageInfo getVideoInfoList(Integer page, Integer size, Integer shopId) {
        PageHelper.startPage(page, size);
        Example example = new Example(VideoInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId", shopId);
        example.orderBy("videoSort").desc();
        return new PageInfo(videoInfoMapper.selectByExample(example));
    }

    @Override
    public void editVideoInfo(VideoInfo videoInfo) {
        if (videoInfo.getVideoId() == null) {
            //添加
            videoInfo.setIsDelete(1);
            videoInfo.setCreateTime(DateFormatHelper.currentTimes());
            videoInfoMapper.insertSelective(videoInfo);
        } else {
            //修改
            videoInfoMapper.updateByPrimaryKeySelective(videoInfo);
        }
    }

    @Override
    public void delVideoInfo(Integer videoId) {
        videoInfoMapper.deleteByPrimaryKey(videoId);
    }

    @Override
    public VideoInfo detail(Integer videoId) {
        return videoInfoMapper.selectByPrimaryKey(videoId);
    }

    @Override
    public void updateSortByVideoId(Integer videoId, Integer sort) {
        VideoInfo videoInfo = new VideoInfo();
        videoInfo.setVideoId(videoId);
        videoInfo.setVideoSort(sort);
        videoInfoMapper.updateByPrimaryKeySelective(videoInfo);
    }

    @Override
    public List<VideoCategory> videoAll(Integer shopId) {
        //查询分类
        List<VideoCategory> list = videoCategoryService.getVideoCateGoryList(shopId);
        list.stream().forEach(map -> map.setVideoInfoList(
                videoCategoryService.queryVideoListByCategoryId(map.getCategoryId())
        ));
        return list;
    }
}
